<template>
  <div class="container scopClass">
    <!-- <el-tabs
      v-model="activeName"
      type="border-card"
      style="margin-top: 20px"
      @tab-click="handleClick"
    >
      <el-tab-pane
        :label="item.templateName"
        :name="item.templateName"
        v-for="(item, index) in List"
        :key="index"
      > -->
        <!-- <el-row style="margin: 20px 0">
          <el-col :span="7">
            <el-input
              style="border-radius: 0 5px 5px 0; width: 300px"
              placeholder="请输入关键词"
              v-model.trim="searchName1"
            >
              <i slot="prefix" class="el-input__icon el-icon-search"></i>
              <el-button slot="append" @click="handleSearch1">搜索</el-button>
            </el-input>
          </el-col>
          <el-button
            type="primary"
            @click="newAdd"
            style="float: right; margin-right: 10px"
            >新增</el-button
          >
        </el-row> -->

		<el-row style="margin:20px 0;">
		    <el-col :span="7">
		        <el-input style="border-radius: 0 5px 5px 0;width:300px;" placeholder="请输入关键词"  v-model.trim="searchName1">
		            <i slot="prefix" class="el-input__icon el-icon-search"></i>
		            <el-button slot="append" @click="handleSearch1">搜索</el-button>
		        </el-input>

		    </el-col>

      <el-button  style="float:right; margin-right: 10px" @click="download12">导出销售备案号模板</el-button>
      <el-upload
          class="upload-daoru"
          ref="upload"
          :action="action"
          :on-change="uploadonchange12"
          accept=".xlsx,.xls"
          :show-file-list='false'
          :auto-upload="false">
        <el-button slot="trigger" size="small">导入销售备案号</el-button>
      </el-upload>

		</el-row>
        <el-table
          :data="tableData"
          border
          ref="multipleTable"
          header-cell-class-name="table-header"
        >
          <el-table-column prop="" sortable label="标识分类" >
            <template slot-scope="scope">
              <span v-if="scope.row.fromFactoryName !== null">{{scope.row.fromFactoryName}}</span>
              <span v-else>{{scope.row.factoryName}}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="factoryName" sortable label="标识分类" ></el-table-column> -->
          <el-table-column prop="inventoryType" sortable label="产品大类"></el-table-column>
          <el-table-column prop="breedName" sortable label="名称"></el-table-column>
          <!-- <el-table-column prop="loadingQuantity" sortable label="装量"></el-table-column>
          <el-table-column prop="loadingUnits" sortable label="包装"></el-table-column> -->
		  <el-table-column prop="ypCodeStandard" sortable label="药品标识码标准"></el-table-column>
		  <el-table-column label="销售备案号" width="150" align="center">
		    <template slot-scope="scope">
		      <el-button
		        type="text"
				style="color:#606266;"
		        @click="handleCheck(scope.$index, scope.row)"
		        >查看</el-button
		      >
		    </template>
		  </el-table-column>
          <el-table-column prop="createTime" sortable label="添加修改时间"></el-table-column>

          <el-table-column label="操作" width="150" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          layout="total, prev, pager, next, jumper"
          :page-size="pagesize1"
          @current-change="handleCurrentChange1"
          :total="total1"
          :current-page.sync="currentPage1"
          style="margin-top: 20px"
        ></el-pagination>




		<el-dialog title="销售备案号" :visible.sync="dialogFormVisible1">
		    <el-table
		      :data="tableData1"
		      border
		      ref="multipleTable"
		      header-cell-class-name="table-header"
		    >

		      <el-table-column prop="address" sortable label="地域名称"></el-table-column>
		      <el-table-column prop="recordCode" sortable label="销售备案号"></el-table-column>

		    </el-table>
		    <div slot="footer" class="dialog-footer">
		        <el-button @click="dialogFormVisible1 = false">关 闭</el-button>
		        <!-- <el-button type="primary" @click="submitForm1('ruleForm1')">确 定</el-button> -->
		    </div>
		</el-dialog>
    <!--  </el-tab-pane>
    </el-tabs> -->
  </div>
</template>

<script>
import axios from "axios";
import { materialBreed_list, materialBreed_delete } from "@/request/api";
import {
  materials_import,
  template_findByCompanyId as templateFindByCompanyId,
  materialBreed_findByLis as materialBreedFindByLis,
} from "@/request/manageapi";
import uploadFile from "../../../common/uploadfile";
import {exportSellRecordTemp} from "../../../../request/api";
export default {
  props: ["activeName1"],
  components: {
    uploadFile,
  },
  data() {
    return {
      tableData: [],
      tableData1: [],
      total1: 0,
      currentPage1: 1,
      pagesize1: 10,
      activeName: "",
      searchName1: "",
      List: [],
	  dialogFormVisible1:false,
      action: '',
    };
  },
  async created() {
    await this.init();
    await this.materialBreedFindByLis();
    // var data = JSON.stringify(this.$store.state.wl);
    // var a = "新增";
    // var b = "编辑";
  },
  mounted() {},
  destroyed: function () {
    this.$store.commit("setStorName", "");
  },
  methods: {
    uploadonchange12(file) {
      let formData = new FormData();
      formData.append('file', file.raw);
      let config = {
        headers:{'Content-Type':'multipart/form-data'}
      };
      exportSellRecordTemp(formData).then((res) =>{
        if (res.code == 200) {
          this.$notify({
            title: '操作成功',
            message: '导入成功',
            type: 'success'
          });
          this.init()
        } else{
          this.$notify.error({
            title: '操作失败',
            message: '导入失败',
            type: 'error'
          });
        }
      })
    },
    download12(){
      axios({
        method: 'GET',
        url: '/materialBreed/exportSellRecordTemp',
        responseType: 'blob'
      }).then(res=>{
        // console.log(res)
        this.downloadFile(res, '数据.xls')
      }).catch(err=>{
        // console.log(err)
      })
    },
    downloadFile(res, fileName) {
      let blob = new Blob([res.data]);
      if (!fileName) {
        fileName = res.headers['content-disposition'].split('filename=').pop();
      }
      if ('msSaveOrOpenBlob' in navigator) {
        window.navigator.msSaveOrOpenBlob(blob, fileName);
      } else {
        const elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href);
        document.body.removeChild(elink);
      }
    },
	  // 打开销售备案号弹窗
	  handleCheck(index,row){
      this.tableData1 = []
		  this.dialogFormVisible1 = true
		  if(row.sellRecord){
			  this.tableData1 = JSON.parse(row.sellRecord)
		  }

	  },
    // 调用接口
    init() {
      // let companyId = this.activeName1;
      let companyId = localStorage.getItem("companyId")
      return new Promise((resolve, reject) => {
        templateFindByCompanyId(`?companyId=${companyId}&&isNew=1`)
          .then((res) => {
            this.List = res.data;
            if (this.List.length > 0) {
              this.activeName = this.List[0].templateName;
            }
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    materialBreedFindByLis() {

      let companyId = this.activeName1;
      let materialType = "配方颗粒";
      let inventoryType = "C60中药配方颗粒,C70中药配方颗粒";
      let page = this.currentPage1;
	  let searchName = this.searchName1
      let data = {
        companyId,
        materialType,
        inventoryType,
        page,
		searchName
      };
      return new Promise((resolve, reject) => {
        materialBreedFindByLis(data).then((res) => {
          if (res.code == 200) {
            this.total1 = res.total
            let tableData = res.rows;
            this.tableData = JSON.parse(JSON.stringify(tableData));
			for(var i=0;i<this.tableData.length;i++){
				if(this.tableData[i].ypCodeStandard == 1){
					this.tableData[i].ypCodeStandard = "国标"
				}else if(this.tableData[i].ypCodeStandard == 2){
					this.tableData[i].ypCodeStandard = "省标"
				}
			}
          }
          console.log(res);
          resolve(res);
        });
      });
    },
    handleSearch1() {
		this.page = 1
		this.total1 = 0
		this.materialBreedFindByLis()
	},
    // tabs
    async handleClick(tab, event) {
      this.currentPage1 = 1
      await this.materialBreedFindByLis();
    },
    // 编辑
    handleEdit(index, row) {
      // console.log(row)
      this.$router.push({
        name:"managerecordAdd1",

         query:{
          id:row.id
        }
      });
    },
    // 分页事件
    async handleCurrentChange1(val) {
      this.currentPage1 = val;
      await this.materialBreedFindByLis();
    },
  },
  watch: {
    activeName1: {
      async handler(newVal, oldVal) {
        this.activeName1 = newVal;
        await this.init();
        await this.materialBreedFindByLis();
      },
      deep: true,
    },
  },
};
</script>

<style lang="less" scoped>
// 深度作用器
/deep/.upload-daoru {
  .el-upload {
    border: none;
    width: auto;
    height: auto;
    border-radius: 0;
    float: right;
    margin-right: 10px;
  }
}
.el-col {
  margin-bottom: 0;
}
/* .scopClass .el-table{ margin-top: 20px;} */
.el-pagination {
  text-align: center;
}
/*
.el-select .el-input {
width: 130px;
}
.input-with-select .el-input-group__prepend {
background-color: #fff;
}
.el-input--small .el-input__inner{
    border-radius: 5px 0 0 5px !important;
    border-right: none;
} */
</style>
